//index.js
//获取应用实例
const app = getApp()
const subjectService = require('../../service/subject.js');

Page({
  data: {
    title:"专题详情",
    id:"",
    detailInfo: {
      subject:{},
      paper:[]
    },
    queryParams: {
      page: 0,
      num: 10
    },
    isLast: false,
    isShowBackTop:false
  },
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      id: id
    })
    let d = {
      subjectId: id
    }
    this.getContentList(d);
  },
  getContentList(d) {
    subjectService.getSubjectDetail(d).then(res => {
      if (res.resCode == 0) {
        this.setData({
          detailInfo: res.object
        });
      }
    })
  },
  getArticleList(){
    let d = {
      ...this.data.queryParams,
      subjectId: this.data.id
    }
    subjectService.getSubjectArticleList(d).then(res => {
      if(res.resCode == 0){
        this.setData({
          'detailInfo.paper':[
            ...this.data.detailInfo.paper,
            ...res.object
          ]
        })
      }
    })
  },
  onReachBottom() {
    if (this.data.isLast) return false;
    this.setData({
      queryParams: {
        page: ++this.data.queryParams.page,
        num: 10,
      }
    })
    this.getArticleList();
  },
  onPageScroll:function(e){
    let deviceInfo = app.globalData.deviceInfo;
    if (e.scrollTop > deviceInfo.screenHeight +50){
      this.setData({
        isShowBackTop:true
      })
    }else{
      this.setData({
        isShowBackTop:false
      })
    }
  },
  getShareCover(str = ""){
    let strArr = str.split(",");
    return `${app.globalData.$config.IMAGE_BASE_URL}${strArr[0]}.jpg`
  },
  getShareTitle(str = "",len = 50){
    return str.slice(0,len);
  },
  onShareAppMessage(options){
    return {
      imageUrl: this.getShareCover(this.data.detailInfo.subject.subjectImages),
      title: this.getShareTitle(this.data.detailInfo.subject.subjectName+'-'+this.data.detailInfo.subject.subjectIntroduce)
    }
  },
})
